﻿#pragma once

#include <Hazal.h>
#include <glm/gtc/type_ptr.hpp>

#include "Map.h"
#include "Panels/ContentBrowserPanel.h"
#include "Panels/SceneHierarchyPanel.h"

class CSandbox2D : public Hazal::CLayer
{
public:
	CSandbox2D();

public:
	void OnAttach() override;

	void OnDetach() override;

	void OnUpdate(float fTimeSpan) override;

	void OnEvent(Hazal::CEvent& event) override;

	void OnImGuiRender() override;

private:
	bool OnKeyPressedEvent(Hazal::CKeyPressedEvent& event);

	bool OnMouseButtonPressedEvent(Hazal::CMouseButtonPressedEvent& event);

	/// <summary>
	///  Create a scene
	/// </summary>
	void NewScene();

	/// <summary>
	/// Open a scene file to memory
	/// </summary>
	void OpenScene();

	/// <summary>
	/// Open a scene file to memory
	/// </summary>
	/// <param name="rPath">Scene file path</param>
	void OpenScene(const std::filesystem::path& rPath);

	/// <summary>
	/// Save a scene to file
	/// </summary>
	void SaveSceneAs();

	/// <summary>
	/// Save the modification to current opend scene
	/// </summary>
	void SaveScene();

	/// <summary>
	/// Duplicate a new entity from the selected entity in scene
	/// </summary>
	void OnDuplicateEntitty();

	/// <summary>
	/// Create ImGui based tool bar. 
	/// </summary>
	void UI_Toolbar();

	/// <summary>
	/// Set current scene state = Edit
	/// </summary>
	void OnSceneEdit();

	/// <summary>
	/// Set current scene state = Play
	/// </summary>
	void OnScenePlay();

private:
	glm::vec2 m_vecViewportPanelPos;		///< 视口面板位置
	glm::vec4 m_vec4Color;					///< TODO: Flat color
	float m_fRotate;						///< TODO: 随Imgui控制的旋转角度

	bool m_bViewportFocused;				///< ImGui视口窗体是否处于聚焦状态
	bool m_bViewportHovered;				///< 鼠标是否悬停于Viewport之上

	glm::vec2 m_aVecViewportBounds[2];		///< Viewport窗体的边界坐标

	int m_nGizmoType = -1;					///< ImGuizmo选项(Translation, Rotation, Scale)
	bool m_bImGuiZmoDrew = false;			///< 获取ImGuiZmo的使用状态

	CMap m_mapTown;							///< TODO: 城镇地图

	Hazal::Ref<Hazal::CTexture2D> m_pTexCheckboard;			///< 棋盘纹理
	Hazal::Ref<Hazal::CTexture2D> m_pTownSpriteSheet;		///< Town Sprite sheet
	Hazal::Ref<Hazal::CSubTexture2D> m_pSubTexTown;			///< Sub texture of TownSpriteSheet

	Hazal::Ref<Hazal::CFrameBuffer> m_pFramebuffer;			///< Framebuffer 

	Hazal::Ref<Hazal::CScene> m_pActiveScene;				///< The active scene based on ECS
	Hazal::Ref<Hazal::CScene> m_pEditScene;					///< The edit scene based on ECS
	std::filesystem::path m_editScenePath;					///< Edit scene path

	Hazal::CEntity m_enHoveredEntity;						///< Hovered entity

	Hazal::COrthographicCameraControll m_cameraController;	///< 正交相机控制器

	Hazal::CSceneHierarchyPanel m_panel;					///< ImGui panels
	Hazal::CContentBrowserPanel m_contBrowerPanel;			///< Asset browser panel

	Hazal::CEditorCamera m_editorCamera;					///< Editor camera

	/// <summary>
	/// Scene state. Play, Edit
	/// </summary>
	enum class ESceneState
	{
		Edit = 0,
		Play = 1,
	};

	ESceneState m_eSceneState = ESceneState::Edit;			///< Save current scene state
	Hazal::Ref<Hazal::CTexture2D> m_pTexScenePlay;			///< "Play" texture
	Hazal::Ref<Hazal::CTexture2D> m_pTexSceneStop;			///< "Stop" texture
};
